package jdbc2;

import myschool.util.DbUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author oldliu
 * @since 1.0
 */
public class QueryRens {
    public static void main(String[] args) {
        String sql="select count(*) as ren,sex from s_student group by sex";
        Connection con= DbUtil.get();
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            ps=con.prepareStatement(sql);
            rs=ps.executeQuery();
            while (rs.next()){
                int renshu=rs.getInt(1);
                String sex=rs.getString(2);
                System.out.println(sex+" 有"+ renshu+"人");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DbUtil.close(rs,ps,con);
        }
    }
}
